/*
点赞 模块
*/

package model

import (
    "gitee.com/keeving/quanta-test-cat/utils/errmsg"
    "gorm.io/gorm"
)

type LikeArticle struct {
    gorm.Model
    ID        int  `gorm:"primaryKey" json:"id"`
    ArticleID uint `gorm:"type:int;not null" json:"article_id"`
    UserID    uint `gorm:"type:int;not null" json:"user_id"`
}

// FirstLike 一级评论的点赞结构体
type FirstLike struct {
    gorm.Model
    ID         uint `gorm:"primary_key;auto_increment" json:"id"` // 评论ID
    CommentID  uint `gorm:"type:int;not null" json:"comment_id"`  // 评论ID
    // ArticleID  uint `gorm:"type:int;not null" json:"article_id"`  // 文章ID
    UserID     uint `gorm:"type:int;not null" json:"user_id"`     // 用户ID
}

// CreateLike 创建点赞
func CreateLike(data *LikeArticle) int {
    if CheckLike(data.ArticleID, data.UserID) == errmsg.ERROR_LIKE_HAS_EXIST {
        return errmsg.ERROR_LIKE_HAS_EXIST
    }
    db.Create(&data)
    db.Model(&Article{}).Where("id = ?", data.ArticleID).UpdateColumn("like_count", gorm.Expr("like_count + ?", 1))
    return errmsg.SUCCESS
}

// DeleteLike 删除点赞
func DeleteLike(articleID uint, userID uint) int {
    var like LikeArticle
    db.Where("article_id = ? AND user_id = ?", articleID, userID).Delete(&like)
    db.Model(&Article{}).Where("id = ?", articleID).UpdateColumn("like_count", gorm.Expr("like_count - ?", 1))
    return errmsg.SUCCESS
}

// GetLike 获取用户的点赞记录
func GetLike(userID int) ([]LikeArticle, int) {
    var likes []LikeArticle
    db.Where("user_id = ?", userID).Find(&likes)
    return likes, errmsg.SUCCESS
}

// CheckLike 检查用户是否已点赞
func CheckLike(articleID uint, userID uint) int {
    var like LikeArticle
    db.Where("article_id = ? AND user_id = ?", articleID, userID).First(&like)
    if like.ID != 0 {
        return errmsg.ERROR_LIKE_HAS_EXIST
    }
    return errmsg.SUCCESS
}

// ToggleLike 切换点赞状态
func ToggleLike(articleID uint, userID uint) int {
    if CheckLike(articleID, userID) == errmsg.ERROR_LIKE_HAS_EXIST {
        return DeleteLike(articleID, userID)
    }
    like := LikeArticle{
        ArticleID: articleID,
        UserID:    userID,
    }
    return CreateLike(&like)
}

// 查询用户点赞的文章
func GetUserLikes(userID uint) ([]Article, int) {
    var likes []LikeArticle
    if err := db.Where("user_id = ?", userID).Find(&likes).Error; err != nil {
        // fmt.Println("Error querying likes:", err)
        return nil, errmsg.ERROR
    }

    var articles []Article
    for _, like := range likes {
        var article Article
        if err := db.First(&article, like.ArticleID).Error; err != nil {
            return nil, errmsg.ERROR
        }
        articles = append(articles, article)
    }

    return articles, errmsg.SUCCESS
}

// FirstCommentToggleLike 切换一级评论的点赞状态
func FirstCommentToggleLike(commentID uint, userID uint) int {
    var firstLike FirstLike

    // 检查用户是否已经点赞
    err := db.Model(&FirstLike{}).Where("comment_id = ? AND user_id = ?", commentID, userID).First(&firstLike).Error
    if err == gorm.ErrRecordNotFound {
        // 用户没有点赞，进行点赞
        newLike := FirstLike{
            ID:        0, // 自动增长
            CommentID: commentID,
            UserID:    userID,
        }
        err = db.Create(&newLike).Error
        if err != nil {
            // fmt.Println("Error creating like:", err)
            return errmsg.ERROR
        }
        // 更新评论的点赞数量
        db.Model(&Comment{}).Where("id = ?", commentID).Update("like_num", gorm.Expr("like_num + 1"))
        return errmsg.SUCCESS
    } else if err != nil {
        // fmt.Println("Error checking like:", err)
        return errmsg.ERROR
    }

    // 用户已经点赞，取消点赞
    err = db.Delete(&firstLike).Error
    if err != nil {
        // fmt.Println("Error deleting like:", err)
        return errmsg.ERROR
    }
    // 更新评论的点赞数量
    db.Model(&Comment{}).Where("id = ?", commentID).Update("like_num", gorm.Expr("like_num - 1"))
    return errmsg.SUCCESS
}